package com.anhuanjia.tools.eao.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import com.anhuanjia.tools.eao.bean.EaoSchedule;
import com.efastsoft.framework.Member;
import com.efastsoft.framework.UIFacade;
import com.efastsoft.framework.annotation.Alias;
import com.efastsoft.framework.annotation.Priv;
import com.efastsoft.framework.annotation.Priv.LoginType;
import com.efastsoft.framework.data.DataRow;
import com.efastsoft.framework.data.DataTable;
import com.efastsoft.framework.data.Q;
import com.efastsoft.framework.data.Transaction;
import com.efastsoft.framework.i18n.Lang;
import com.efastsoft.framework.utility.StringUtil;

/**
 * 日程操作接口
 * 
 * @author Administrator
 *
 */
@Alias(value = "schedule")
public class ScheduleUI extends UIFacade {
	/**
	 * 添加日程信息
	 */
	@Priv(loginType = LoginType.Member)
	public void insert() {
		String member = Long.toString(Member.getUID()); // 默认为登录人
		String content = $V("Content");
		String do_time = $V("DoTime");
		if (StringUtil.isEmpty(member) || StringUtil.isEmpty(content) || StringUtil.isEmpty(do_time)) {
			f("操作失败");
			return ;
		}
		Date date = new Date();
		Date dodate = null;
		Date nowdate = null;
		SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd"); // 加上时间
		SimpleDateFormat DateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 加上时间
		try {
			dodate = sDateFormat.parse(do_time);
			nowdate = DateFormat.parse(DateFormat.format(date));
		} catch (ParseException px) {
			px.printStackTrace();
		}
		Transaction transaction = new Transaction();
		EaoSchedule entity = new EaoSchedule();
		entity.setId(UUID.randomUUID().toString());
		entity.setMember(member);
		entity.setContent(content);
		entity.setDoTime(dodate);
		entity.setCreateTime(nowdate);
		entity.setModifyTime(nowdate);
		transaction.insert(entity);
		if (transaction.commit()) {
			s("添加成功");
		} else {
			f("添加失败");
		}
	}


	/**
	 * 修改日程
	 */
	@Priv(loginType = LoginType.Member)
	public void update() {
		Transaction transaction = new Transaction();
		EaoSchedule entity = new EaoSchedule();
		entity.setValue(this.Request);
		String id = entity.getId();
		if (StringUtil.isEmpty(id)) {
			String member = Long.toString(Member.getUID()); // 默认为登录人
			String content = $V("Content");
			String do_time = $V("DoTime");
			if (StringUtil.isEmpty(member) || StringUtil.isEmpty(content) || StringUtil.isEmpty(do_time)) {
				f("操作失败");
				return;
			}
			Date date = new Date();
			Date dodate = null;
			Date nowdate = null;
			SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd"); // 加上时间
			SimpleDateFormat DateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 加上时间
			try {
				dodate = sDateFormat.parse(do_time);
				nowdate = DateFormat.parse(DateFormat.format(date));
			} catch (ParseException px) {
				px.printStackTrace();
			}
			EaoSchedule entitys = new EaoSchedule();
			entitys.setId(UUID.randomUUID().toString());
			entitys.setMember(member);
			entitys.setContent(content);
			entitys.setDoTime(dodate);
			entitys.setCreateTime(nowdate);
			entitys.setModifyTime(nowdate);
			transaction.insert(entitys);
			if (transaction.commit()) {
				s("添加成功");
			} else {
				f("添加失败");
			}
		} else {
			DataTable dt = new Q("select * from eao_schedule where Id =? ", id).fetch();
			EaoSchedule old = new EaoSchedule();
			old.setValue(dt.get(0));
			old.setValue(this.Request);
			Date date = new Date();
			SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date nowdate = null;
			try {
				nowdate = sd.parse(sd.format(date));
			} catch (ParseException e) {
				e.printStackTrace();
			}
			old.setModifyTime(nowdate);
			transaction.update(old);
			if (transaction.commit()) {
				s("修改成功");
			} else {
				f("修改失败");
			}
		}
	}

	/**
	 * 删除日程
	 */
	@Priv(loginType = LoginType.Member)
	public void delete() {
		String id = $V("Id");
		if (StringUtil.isEmpty(id)) {
			f("操作失败");
			return;
		}
		Transaction trans = new Transaction();
		Q q2 = new Q(" delete from eao_schedule  where Id = ?", id);
		trans.add(q2);
		if (trans.commit()) {
			s(Lang.get("Common.ExecuteSuccess"));
		} else {
			f(Lang.get("Common.ExecuteFailed"));
		}
	}

	/**
	 * 获取日程列表
	 * 
	 * @throws Exception
	 */
	@Priv(loginType = LoginType.Member)
	public void selectAll() throws Exception {
		// String member = $V("Member");
		String member = Long.toString(Member.getUID()); // 默认为登录人
		String month = $V("Month");
		if (StringUtil.isEmpty(month)) {
			f("操作失败");
			return;
		}
		Q q = new Q("select Id,Content,DoTime from eao_schedule where Member = ?", member);
		q.append(" AND DATE_FORMAT(DoTime,'%Y-%m-%d') LIKE ?", "%" + month + "%");
		DataTable dt = q.fetch();
		$S("data", dt);
	}

	/**
	 * 查询是否到达日程
	 */
	@Priv(loginType = LoginType.Member)
	public void thisTime() {
		// String member = $V("Member");
		String member = Long.toString(Member.getUID()); // 默认为登录人
		Date date = new Date();
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
		Date nowdate = null;
		try {
			nowdate = sd.parse(sd.format(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		DataTable dt = new Q("select count(*) as c from eao_schedule where DoTime =? and Member =?", nowdate, member)
				.fetch();
		if(dt.getRowCount()!=0) {
			int count = 0;
			for (DataRow dr : dt) {
				count = dr.getInt(0);
			}
			if (count > 0) {
				DataTable dt2 = new Q("select * from eao_schedule where DoTime =? and Member =?", nowdate, member).fetch();
				$S("data", dt2);
			} else {
				f("今日无日程安排");
				return;
			}
		}
	}
	
	@Priv(loginType = LoginType.Member)
	public void findTime() {
		// String member = $V("Member");
		String member = Long.toString(Member.getUID()); // 默认为登录人
		String  nowdate = $V("DoTime");
		if(StringUtil.isNotEmpty(member)||StringUtil.isNotEmpty(nowdate)) {
			DataTable dt = new Q("select count(*) as c from eao_schedule where DoTime =? and Member =?", nowdate, member)
					.fetch();
			if(dt.getRowCount()!=0) {
				int count = 0;
				for (DataRow dr : dt) {
					count = dr.getInt(0);
				}
				if (count > 0) {
					DataTable dt2 = new Q("select * from eao_schedule where DoTime =? and Member =?", nowdate, member).fetch();
					$S("data", dt2);
				} else {
					f("请输入要添加的日程信息");
					return;
				}
			}
		}
	}

	/**
	 * 清空所有日程安排
	 */
	@Priv(loginType = LoginType.Member)
	public void clearData() {
		String member = Long.toString(Member.getUID()); // 默认为登录人
		String month = $V("Month");
		if(StringUtil.isNotEmpty(member)||StringUtil.isNotEmpty(month)) {
			Transaction trans = new Transaction();
			Q q2 = new Q(" delete from eao_schedule  where Member = ?", member);
			q2.append(" AND DATE_FORMAT(DoTime,'%Y-%m-%d') LIKE ?", "%" + month + "%");
			trans.add(q2);
			if (trans.commit()) {
				s(Lang.get("Common.ExecuteSuccess"));
			} else {
				f(Lang.get("Common.ExecuteFailed"));
			}
		}
	}
}
